package commands;

import application.Core;

	/**
 *	This class implements command man.
 * 
 * @author Michal Mourek, Josef Vopalecký
 */
public class Man extends AbstractCommand{
	/**
	 * Main method of this program.
	 * It print help specified command or all commands.
	 *
	 * @param parameters List of parameters entered by user in command line.
	 * @param arguments List of arguments entered by user in command line.
	 * @param id Process ID.
	 * @param core reference to the core
	 * @author Michal Mourek, Josef Vopalecký
	 */
	@Override
	public void main(String[] parameters, String[] arguments, int id, Core core) {
		if (parameters.length == 0){
			if (arguments.length != 0){
				core.getHelp(arguments[0]);
			} 
			else {
				String[] impCom = implementedCommands().split("\n");
				for (String string : impCom) {
					core.write(id, string);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		} else{
			for (int i = 0; i < parameters.length; i++) {
				char parameter = parameters[i].charAt(1);
				if (parameter == 'h'){
					String[] help = help().split("\n");
					for (String string : help) {
						core.write(id, string);
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				} else{
					String[] invPar = invalidParameters().split("\n");
					for (String string : invPar) {
						core.write(id, string);
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					break;
				}
			}
		}
	}
	
	/**
	 * Print help for this command.
	 *
	 * @author Michal Mourek
	 */
	@Override
	public String help() {
		return ("NAME: man - print helps \n SYNOPSYS: man [OPTION] \n \n Description: \n   -h      print help \n");
		//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	}
	
	/**
	 * Print implementied commads.
	 *
	 * @author Michal Mourek
	 */
	public String implementedCommands(){
		return "Cat\nCd\nEcho\nExit\nKill\nLs\nMan\nPs\nPwd\nShel\nShutdown\nSort\n";
	}
	
	/**
	 * Print warning message, when parameter is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidParameters(){
		return ("Parameters is not correct \n Read help man - h \n");
	}
	
	/**
	 * Print warning message, when argument is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidArguments(){
		return ("Arguments is not correct \n Read help man - h \n");

	}
	
}
